package com.xxh.workQueue;

import com.rabbitmq.client.*;
import com.xxh.utils.MqUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = MqUtils.getConnection();
        Channel channel = connection.createChannel();
        //一次消费一个消息
        channel.basicQos(1);
        channel.queueDeclare("testwork", false, false, false, null);
        try {
            TimeUnit.SECONDS.sleep(2);
            /**
             * 1 队列名称
             * 2 是否自动确认
             * 3 消息的回调函数
             */
            channel.basicConsume("testwork", false, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("我是消费者1：" + new String(body));
                    /**
                     * 1 确认消息是哪一条？
                     * 2 一次确认多个消息？FALSE就是每次确认一个消息
                     */
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
